#include <iostream>
#include <string.h>

using namespace std;

struct ListNode 
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
 
class Solution 
{
public:
    void InsertTail(int val)
    {
        ListNode * newNode = new ListNode;
        newNode->val = val;
        newNode->next = nullptr;
        
        ListNode * travelPoint = head;
        while(travelPoint->next != nullptr)//不是最后一个节点
        {
            travelPoint = travelPoint->next;//节点继续往后走
        }
        travelPoint->next = newNode;//指向新插入的节点
        size++;

        
    }

    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) 
    {
        ListNode * result;
        ListNode * p1 = list1;
        ListNode * p2 = list2;
        while (p1 && p2)
        {
            if(p1->val < p2->val)
            {
                InsertTail(p1->val);
                p1 = p1->next;
            }
            else
            {
                InsertTail(p2->val);
                p2 = p2->next;
            }
        }
        while (p1)//p2为nullptr但是p1不是
        {
            InsertTail(p1->val);
            p1 = p1->next;
        }
        while(p2)//p1为nullptr但是p2不是
        {
            InsertTail(p2->val);
            p2 = p2->next;
        }
        return result;
            
    }
private:
    ListNode * head;//头指针  //找到头部就找到了后面的
    int size;//节点数量
    
};